"""
@name : zuoye
@author : chenlu
@projectname: python代码文件
"""
import json

class User:
    user = {}

    def __init__(self, name, passwd, money):

        self.name = name
        self.passwd = passwd
        self.money = money


    def buy_goods(self, good, weight):

        if goods not in list(Goods.goodsdit.keys()):
            print("不好意思该商品没有上架")

        elif float(weight) > Goods.goodsdit[good].store:
            print("该商品已经售卖完了哦")
        else:
            pay = float(weight) * Goods.goodsdit[good].price
            if self.money >= pay:
                self.money -= pay
                Goods.goodsdit[good].store -= float(weight)
                print(f"购买成功，{self.name}剩余{self.money}元， "
                      f"{good}剩余库存{Goods.goodsdit[good].store}kg")
            else:
                print("余额不足")

    def login(self, newname, newpasswd, newmoney):
        if len(User.user) == 0:
            self.serialname = 0
        else:
            userkey = list(User.user.keys())
            self.serialname = userkey[-1] + 1
        newuser = User(newname, newpasswd, newmoney)
        newdict = {newuser.serialname: newuser}
        User.user.update(newdict)
        print(User.user)

    def register(self, serialname, name, passwd):
            # 登录的时候进行判断，3次机会

        # for i in range(3):
        #     if i <= 2:
        #         i += 1
                # print(list(User.user.keys()))

                if serialname in list(User.user.keys()):

                    if User.user[serialname].name == name and User.user[serialname].passwd == passwd:
                        print("登录成功")
                        # break
                else:
                    print("请先注册")
                    # break


class Goods:
    goodsdit = {}

    def __init__(self, classify, product_name, price, store):
            self.classify = classify
            self.product_name = product_name
            self.price = price
            self.store = store

    def addgoods(self, classify, product_name, price, store):
            newgoods = Goods(classify, product_name, price, store)
            newdict = {product_name: newgoods}
            Goods.goodsdit.update(newdict)



user0 = User.login(User, "zhaosiyuan", "123456", 900)
user1 = User.login(User, "quanhongyan", "123456", 500)
user2 = User.login(User, "chenlu", "123456", 800)


apple = Goods.addgoods(Goods, "fruits", "apple", 3, 80)
orange = Goods.addgoods(Goods, "fruits", "orange", 4, 100)
waterlemon = Goods.addgoods(Goods, "fruits", "waterlemon", 6, 120)
banana = Goods.addgoods(Goods, "fruits", "banana", 2, 50)


while 1:
        print("欢迎来到三乐商店".center(40, "*"))
        print("请选择需要进行的操作".center(36, " "))
        print("1.注册".center(40, "*"))
        print("2.登录".center(40, "*"))
        print("3.退出".center(40, "*"))
        print("4.查询购物信息".center(38, "*"))
        select = input("请输入你的选择:")

        if select == "1":
            newname = input("请输入你的名字:")
            newpasswd = input("请输入你的密码:")
            newmoney = float(input("请输入你的存入金额:"))
            User.login(User, newname, newpasswd, newmoney)
            print("注册成功返回首页登录")


            print(list(User.user.keys()))
            continue

        elif select == "2":
            serialname = int(input("请输入序号:"))
            name = input("请输入你的账户:")
            passwd = input("请输入你的密码:")
            User.register(User, serialname, name, passwd)

            if serialname in list(User.user.keys()):

                if User.user[serialname].name == name and User.user[serialname].passwd == passwd:
                    goods = input("请输入需要购买的商品名称:")
                    weight = float(input("请输入需要购买商品的重量(kg):"))
                    User.buy_goods(User.user[serialname], goods, weight)
                else:
                    print("账号或密码错误，请重新登录")

            # else:
            #     print("请先注册")

        elif select == "3":
            print("购买多多，优惠多多")
            inp = input("不退出or退出:")
            if inp == "不退出":
                print("欢迎继续选购")
            else:
                print("欢迎下次光临")
                break

        elif select == "4":
            print(f"购买的商品{goods}")



# def reverse01(head):
#     if head == None:
#         return None
# # 分别用p,q两个指针指定先后两个节点
#     p = head
#     q = head.next
#
# # 将p节点反转，head节点只能指向None
#     p.next = None
#
# # 当存在多个后续节点时，循环执行
#     while q:
#         r = q.next  # 用r表示后面未反转节点
#         q.next = p  # q节点反转指向p
#         p = q
#         q = r  # p,q节点后移一位，循环执行后面的操作
#
#         return p

